package com.corpgovernment.supplier.vo;

import com.corpgovernment.api.supplier.soa.constant.ProductTypeEnum;
import com.corpgovernment.api.supplier.vo.*;
import com.corpgovernment.supplier.entity.db.MbSupplierExtendInfo;
import com.corpgovernment.supplier.entity.db.MbSupplierInfo;
import com.corpgovernment.supplier.service.MbSupplierInfoService;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class SupplierDataModelMapper {
    public MbSupplierInfoVo map2SupplierInfo(MbSupplierInfo msi, AtUserLoginVo userLogin, MbSupplierExtendInfo extendInfo) {
        MbSupplierInfoVo si = new MbSupplierInfoVo();
        if (userLogin != null) {
            BeanUtils.copyProperties(userLogin, si);
        }
        if (extendInfo != null) {
            BeanUtils.copyProperties(extendInfo, si);
        }
        BeanUtils.copyProperties(msi, si);
        si.setProduct(ProductTypeEnum.getProductList(msi.getSupportProduct())
                .stream().filter(p -> !ProductTypeEnum.i18n_flight.name().equals(p)).collect(Collectors.toList()));
        si.setServerTypes(loadSrvTypes(msi.getServiceType()));
        return si;
    }

    public MbSupplierInfoService.SupplierInfo map2Entity(MbSupplierInfoVo si) {
        MbSupplierInfo msi = new MbSupplierInfo();
        BeanUtils.copyProperties(si, msi);
        msi.setSupportProduct(ProductTypeEnum.getOwnedProduct(si.getProduct()));
        msi.setServiceType(loadSrvType(si.getServerTypes()));
        MbSupplierExtendInfo msei = new MbSupplierExtendInfo();
        BeanUtils.copyProperties(si, msei);
        AtUserLoginVo ul = new AtUserLoginVo();
        BeanUtils.copyProperties(si, ul);
        return new MbSupplierInfoService.SupplierInfo(msi, msei, ul);
    }

    public SupplierInfoVoOld map2Old(MbSupplierInfoVo siv) {
        SupplierInfoVoOld sivo = new SupplierInfoVoOld();
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setAdminName(siv.getUid());
        adminInfo.setCountryCode(siv.getCountryCode());
        adminInfo.setName(siv.getUserName());
        adminInfo.setPassword(siv.getPwd());
        adminInfo.setPhone(siv.getPhoneNum());
        sivo.setAdminInfo(adminInfo);
        SupplierExtendInfo extendInfo = new SupplierExtendInfo();
        BeanUtils.copyProperties(siv, extendInfo);
        extendInfo.setIsDeleted(0);
        sivo.setSupplierExtendInfo(extendInfo);
        BeanUtils.copyProperties(siv, sivo);
        sivo.setIsFreeze(siv.getIsFreeze() ? 1 : 0);
        sivo.setIsDeleted(0);
        return sivo;
    }

    public MbSupplierInfoVo map2New(SupplierInfoVoOld sivOld) {
        MbSupplierInfoVo siv = new MbSupplierInfoVo();
        siv.setUid(sivOld.getAdminInfo().getAdminName());
        siv.setCountryCode(sivOld.getAdminInfo().getCountryCode());
        siv.setUserName(sivOld.getAdminInfo().getName());
        siv.setPwd(sivOld.getAdminInfo().getPassword());
        siv.setPhoneNum(sivOld.getAdminInfo().getPhone());
        BeanUtils.copyProperties(sivOld.getSupplierExtendInfo(), siv);
        BeanUtils.copyProperties(sivOld, siv);
        siv.setIsFreeze(sivOld.getIsFreeze() == 1);
        return siv;
    }

    public List<String> loadSrvTypes(String srvType) {
        if (StringUtils.isBlank(srvType)) {
            return Collections.emptyList();
        }
        switch (srvType.toUpperCase()) {
            case "OWN":
                return Collections.singletonList("OWN");
            case "PUB":
                return Collections.singletonList("PUB");
            case "ALL":
                return Arrays.asList("OWN", "PUB");
            default:
                return Collections.emptyList();
        }
    }

    private String loadSrvType(List<String> srvTypes) {
	    if (CollectionUtils.isEmpty(srvTypes)) {
		    return null;
	    }
	    boolean own = srvTypes.stream().anyMatch("OWN"::equalsIgnoreCase);
	    boolean pub = srvTypes.stream().anyMatch("PUB"::equalsIgnoreCase);
	    if (own && pub) {
		    return "ALL";
	    } else if (own) {
		    return "OWN";
	    } else if (pub) {
		    return "PUB";
	    }
	    return null;
    }

}
